﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

using QuDao;
using FineUICore;
using Kalman;
using Kalman.Extensions;

namespace AdminUI.Pages.SysManage
{
    public partial class RoleModel : BaseModel
    {
        public void OnGet()
        {
            var req = new PagingRequest
            {
                PageIndex = 1,
                PageSize = 20
            };
            var result = RoleBO.Instance.Query(req);
            ViewBag.DataSource = result.Data;
            ViewBag.Total = result.Total;
            ViewBag.PageSize = result.PageSize;
        }

        /// <summary>
        /// 执行会引起表格控件数据变化的相关操作
        /// </summary>
        /// <param name="searchCondition">搜索条件</param>
        /// <param name="searchText">搜索关键字文本</param>
        /// <param name="selectedIDs">已选记录ID数组</param>
        /// <param name="Grid1_fields">当前表格控件所有列名称数组（注意：事件触发控件需要指定表格所在容器ID）</param>
        /// <returns></returns>
        public IActionResult OnPostGrid_DoAction(string actionType, string searchCondition, string searchText, int[] selectedIDs,
            int ddlGridPageSize, int Grid1_pageIndex, string Grid1_sortField, string Grid1_sortDirection, string[] Grid1_fields)
        {
            var grid = UIHelper.Grid("Grid1");
            var req = new PagingRequest
            {
                PageIndex = Grid1_pageIndex + 1,
                PageSize = ddlGridPageSize,
                SortExp = $"{Grid1_sortField} {Grid1_sortDirection}" //数据库排序，也可以对分页结果再排序
            };

            switch (actionType)
            {
                case "search":
                    if (!string.IsNullOrEmpty(searchText) && !string.IsNullOrEmpty(searchCondition))
                    {
                        req.FilterItems = new List<FilterItem> { new FilterItem { Field = searchCondition, Value = searchText } };
                    }
                    break;
                case "delete":
                    RoleBO.Instance.Delete(selectedIDs[0]);
                    LogAction(ActionType.删除, $"删除角色：{selectedIDs[0]}");
                    break;
                case "enable":
                    RoleBO.Instance.SetEnable(selectedIDs[0], true);
                        LogAction(ActionType.启用, $"启用角色：{selectedIDs[0]}");
                    break;
                case "disable":
                    RoleBO.Instance.SetEnable(selectedIDs[0], false);
                        LogAction(ActionType.禁用, $"禁用角色：{selectedIDs[0]}");
                    break;
                default:
                    break;
            }

            var result = RoleBO.Instance.Query(req);

            if (actionType == "changePageSize") grid.PageSize(ddlGridPageSize);
            grid.RecordCount(result.Total);
            grid.DataSource(result.Data, Grid1_fields);

            return UIHelper.Result();
        }
    }
}
